package org.ricks.condition2;

import org.ricks.condition2.event.PlayerAwareEvent;
import org.ricks.condition2.event.ProgressUpdatedEvent;
import org.ricks.ioc.event.EventBus;
import java.util.Collection;
import java.util.Comparator;
import java.util.function.BiFunction;
import java.util.function.Predicate;

public class GenericProgressCondition<E extends PlayerAwareEvent, T> extends ProgressCondition<E, T> {

    private final BiFunction<T, E, T> updateLogic;

    public GenericProgressCondition(
            String playerId,
            T initialValue,
            T targetValue,
            Predicate<E> eventFilter,
            BiFunction<T, E, T> updateLogic,
            Comparator<T> comparator,
            Collection<Class<? extends E>> eventTypes,
            ComparisonOperator operator,
            Class<? extends E> eventClass

    ) {
        super(playerId, targetValue, comparator, eventFilter, eventTypes, operator, eventClass);
        this.currentValue = initialValue;
        this.updateLogic = updateLogic;
    }

    @Override
    protected void handleEvent(E event) { // 精确类型参数
        T newValue = updateLogic.apply(currentValue, event);
        if (!newValue.equals(currentValue)) {
            currentValue = newValue;
            EventBus.post(new ProgressUpdatedEvent<>(this, currentValue));
        }
    }
    @Override
    public String getPlayerId() {
        return playerId;
    }

}
